package com.erps.sys.rest;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.erps.CommonUtils.DateUtils;
import com.erps.CommonUtils.StringEx;
import com.erps.dao.ErspCodeValueDao;
import com.erps.dao.ErspResInfoDao;
import com.erps.dao.ErspResPictureDao;
import com.erps.db.DBManager;
import com.hmkj.entity.ErspCodeType;
import com.hmkj.entity.ErspResInfo;
import com.hmkj.entity.ErspResPicture;

public class ErspResInfoRest {
	public int Count(int iStartDay, int iFinishDay) {
		String cStartDay = String.valueOf(iStartDay) + "000000";
		String cFinishDay = String.valueOf(iFinishDay) + "999999";
		return Count(cStartDay, cFinishDay);
	}

	public int Count(String cStartDay, String cFinishDay) {
		StringBuffer sql = new StringBuffer();
		sql.append(" select count(1) from ersp_res_info ");
		sql.append(" where create_time>'" + cStartDay + "'");
		sql.append(" and create_time<'" + cFinishDay + "'");
		return StringEx.getInt(DBManager.GetStrValue(sql.toString()));
	}

	public int Count() {
		StringBuffer sql = new StringBuffer();
		sql.append(" select count(1) from ersp_res_info ");
		sql.append(" where res_release=1 ");
		return StringEx.getInt(DBManager.GetStrValue(sql.toString()));
	}

	public int CountLast() {
		Date vDate = new Date();
		String cStartDay;
		String cFinishDay;
		int iMonth = vDate.getMonth();
		if (iMonth > 6) {
			cStartDay = DateUtils.getDateStr(new Date(), "yyyy") + "0000000000";
			cFinishDay = DateUtils.getDateStr(new Date(), "yyyy") + "0800000000";
		} else {
			String cYear = DateUtils.getDateStr(new Date(), "yyyy");
			cFinishDay = cYear + "0000000000";
			int iYear = StringEx.getInt(cYear) - 1;
			cStartDay = String.valueOf(iYear) + "0800000000";
		}
		return Count(cStartDay, cFinishDay);
	}

	public ErspResInfo FindOne(String cKeyID) {
		StringBuffer sql = new StringBuffer();
		sql.append(" select * ");
		sql.append(" from ersp_res_info ");
		sql.append(" where id='" + cKeyID + "'");
		ErspResInfo vo = new ErspResInfo();

		Map<String, String> rs = DBManager.GetRow(sql.toString());
		vo.setId(StringEx.getMapVal(rs, "id"));
		vo.setOrg_id(StringEx.getMapVal(rs, "org_id"));
		vo.setRes_title(StringEx.getMapVal(rs, "res_title"));
		vo.setRes_contact(StringEx.getMapVal(rs, "res_contact"));
		vo.setRes_contact_phone(StringEx.getMapVal(rs, "res_contact_phone"));
		vo.setRes_summy(StringEx.getMapVal(rs, "res_summy"));
		vo.setRes_desc(StringEx.getMapVal(rs, "res_desc"));
		vo.setRes_release(Short.valueOf(StringEx.getMapVal(rs, "res_release")));
		vo.setRes_order_count(StringEx.getMapVal(rs, "res_order_count"));
		vo.setRes_use_count(StringEx.getInt(StringEx.getMapVal(rs, "res_use_count")));
		vo.setRes_view_count(StringEx.getInt(StringEx.getMapVal(rs, "res_view_count")));
		vo.setRes_fav_count(StringEx.getInt(StringEx.getMapVal(rs, "res_fav_count")));
		vo.setRes_star_count(StringEx.getInt(StringEx.getMapVal(rs, "res_star_count")));
		vo.setRes_checked_user(StringEx.getMapVal(rs, "res_checked_user"));
		vo.setRes_checked(StringEx.getMapVal(rs, "res_checked"));
		vo.setRes_checked_response(StringEx.getMapVal(rs, "res_checked_response"));
		vo.setRes_type(StringEx.getInt(StringEx.getMapVal(rs, "res_type")));
		vo.setRes_position(StringEx.getMapVal(rs, "res_position"));
		try {
			vo.setCreate_time(Long.parseLong(StringEx.getMapVal(rs, "create_time")));
			vo.setRes_posx(Double.parseDouble(StringEx.getMapVal(rs, "res_posx")));
			vo.setRes_posy(Double.parseDouble(StringEx.getMapVal(rs, "res_posy")));
		} catch (Exception ex) {

		}
		vo.setRes_syspath(StringEx.getMapVal(rs, "res_syspath"));
		return vo;

	}

	public PageData Query(int iPageNO, int iPageSize, String cWhereParm) {
		StringBuffer sql = new StringBuffer();
		sql.append(" select count(1) ");
		sql.append(" from ersp_res_info ");
		if (StringEx.getStr(cWhereParm).length() > 0) {
			sql.append(" where  " + cWhereParm);
		}
		// 添加Where条件

		String cOrderBy = " order by last_update_time desc ";

		int iRowCount = StringEx.getInt(DBManager.GetStrValue(sql.toString()));
		int iPageCount = (int) Math.ceil(iRowCount / (double) iPageSize);
		if (iPageNO < 0)
			iPageNO = 1;
		else if (iPageNO > iPageCount)
			iPageNO = iPageCount;

		sql.setLength(0);
		sql.append(" select id,res_no,res_summy,res_desc,org_id,res_title,res_type,last_update_time ");
		sql.append(" from ersp_res_info ");
		if (StringEx.getStr(cWhereParm).length() > 0) {
			sql.append(" where  " + cWhereParm);
		}

		ErspSysOrgRest rest = new ErspSysOrgRest();
		List<Map<String, String>> dtRows = DBManager.QueryData(sql.toString(), cOrderBy, iPageNO, iPageSize);
		PageData ret = new PageData();
		ret.setRowcount(iRowCount);
		ret.setPagecount(iPageCount);
		ret.setPageno(iPageNO);

		ErspResPictureDao vIMG = new ErspResPictureDao();
		ErspCodeValueRest vCode = new ErspCodeValueRest();
		int iLength = (dtRows == null) ? 0 : dtRows.size();
		for (int i = 0; i < iLength; i++) {
			Map<String, String> vo = dtRows.get(i);
			String cOrgID = StringEx.getMapVal(vo, "org_id");
			String cResID = StringEx.getMapVal(vo, "id");
			String cRes_Type = StringEx.getMapVal(vo, "res_type");
			int resType = StringEx.getInt(cRes_Type);
			vo.put("org_id_name", rest.FindOne(cOrgID).getOrg_name());
			vo.put("res_type_name", vCode.FindItem(resType).getCode_value());
			ErspResPicture imgVO = vIMG.FindLast(cResID);
			if (imgVO != null) {
				vo.put("img_url", imgVO.getPic_path());
			}
			ret.getKeylist().add(vo);
		}
		return ret;
	}

	public int ChangeApproval(String cID, int iTypeID, String cUser) {
		ErspResInfoDao dao = new ErspResInfoDao();
		return dao.ChangeApproval(cID, iTypeID, cUser);
	}
}